import {
  queryDefineGroup,
  addDefineGroup,
  removeDefineGroup,
  updateDefineGroup,
  queryIdDefineGroup,
  queryCustomOrgUserTree,
  queryAdmin,
  queryPageListByOrg,
} from '@/services/bnsConfig/defineGroup';
import { queryOrgList, queryTreeOrgList } from '@/services/system/sysUser';
import {queryUserList,} from '@/services/system/sysRole';

export default {
  namespace: 'defineGroup',

  state: {
    // 列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    treeDataList: [],


  },

  effects: {
    *queryList({ payload }, { call, put }) {
      const response = yield call(queryDefineGroup, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addDefineGroup, payload);
      yield put({
        type: 'saveadd',
        payload: response,
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeDefineGroup, payload);
      yield put({
        type: 'saveremove',
        payload: response,
      });
      if (callback) callback(response);
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updateDefineGroup, payload);
      yield put({
        type: 'saveupdate',
        payload: response,
      });
      if (callback) callback(response);
    },
    *queryId({ payload, callback }, { call, put }) {
      const response = yield call(queryIdDefineGroup, payload);
      yield put({
        type: 'savequeryId',
        payload: response,
      });
      if (callback) callback(response);
    },
    *customOrgUserTree({ payload, callback }, { call, put }) {
      const response = yield call(queryCustomOrgUserTree, payload);
      yield put({
        type: 'saveTree',
        payload: response,
      });
      if (callback) callback(response);
    },

    *admin({ payload, callback }, { call, put }) {
      const response = yield call(queryAdmin, payload);
      yield put({
        type: 'saveadmin',
        payload: response,
      });
      if (callback) callback(response);
    },
    *getList({payload, callback}, {call, put}) {
      // const response = yield call(queryOrgList, payload);
      const response = yield call(queryPageListByOrg, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) callback(response);
    },

    * queryUserList({payload, callback}, {call, put}) {
      // const response = yield call(queryOrgList, payload);
      const response = yield call(queryUserList, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) callback(response);
    },
    * getTree({payload}, {call, put}) {
      const response = yield call(queryTreeOrgList, payload);
      yield put({
        type: 'saveTree',
        payload: response,
      });
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    saveTree(state, action) {
      return {
        ...state,
        treeDataList: action.payload.data,
      };
    },
  },
};
